<!DOCTYPE html>
<html th:lang="${#locale.toLanguageTag}" xmlns:th="https://www.thymeleaf.org"
      th:fragment="error_fragment">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>[[${#messages.msg('error.browser.title', error.status, #strings.defaultString(error.title, 'Internal server error'))}]]</title>
    <link rel="icon" th:href="${site.favicon}">
    <style>
        :root {
            --bg-color: #f0f2f5;
            --text-color: #333;
            --card-bg: white;
            --heading-color: #2c3e50;
            --link-color: #3498db;
        }

        .dark-mode {
            --bg-color: #1a1a1a;
            --text-color: #e0e0e0;
            --card-bg: #2c2c2c;
            --heading-color: #ffffff;
            --link-color: #63b3ed;
        }

        body {
            font-family: Arial, sans-serif;
            background-color: var(--bg-color);
            color: var(--text-color);
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            padding: 20px;
            box-sizing: border-box;
            transition: background-color 0.3s, color 0.3s;
        }

        .container {
            background-color: var(--card-bg);
            border-radius: 8px;
            padding: 40px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            text-align: center;
            max-width: 400px;
            width: 100%;
            transition: background-color 0.3s;
        }

        h1 {
            color: var(--heading-color);
            margin-bottom: 20px;
            font-size: 2.5em;
            transition: color 0.3s;
        }

        p {
            line-height: 1.6;
            margin-bottom: 20px;
        }

        .icon {
            font-size: 64px;
            margin-bottom: 20px;
        }

        .back-link {
            color: var(--link-color);
            text-decoration: none;
            font-weight: bold;
            transition: color 0.3s;
        }

        .back-link:hover {
            text-decoration: underline;
        }

        .theme-toggle {
            position: absolute;
            top: 20px;
            right: 20px;
            background: none;
            border: none;
            color: var(--text-color);
            cursor: pointer;
            font-size: 24px;
            transition: transform 0.3s;
        }

        .theme-toggle:hover {
            transform: scale(1.1);
        }

        @media (max-width: 480px) {
            .container {
                padding: 20px;
            }
        }
    </style>
</head>
<body>
<button class="theme-toggle" onclick="toggleTheme()">🌓</button>
<div class="container">
    <div class="icon">🔍</div>
    <h1>[[${error.status + ' ' + #strings.defaultString(error.title, 'Internal server error')}]]</h1>
    <p th:if="${error.status >= 500 && error.status < 600}">[[#{error.concise.tip1}]]</p>
    <p th:if="${error.status >= 400 && error.status < 500}">[[#{error.concise.tip2}]]</p>
    <p th:if="${error.status >= 400 && error.status < 500}">[[#{error.concise.tip3}]]</p>
    <a th:if="${error.status >= 400 && error.status < 500}" href="/" class="back-link" th:aria-label="#{error.concise.button}" th:title="#{error.concise.button}">[[#{error.concise.button}]]</a>
</div>

<script>
  function toggleTheme() {
    document.body.classList.toggle('dark-mode')
    localStorage.setItem('theme', document.body.classList.contains('dark-mode') ? 'dark' : 'light')
  }

  const savedTheme = localStorage.getItem('theme')
  if (savedTheme === 'dark') {
    document.body.classList.add('dark-mode')
  }

  const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)')

  function handleThemeChange(e) {
    if (savedTheme) return
    if (e.matches) {
      document.body.classList.add('dark-mode')
    } else {
      document.body.classList.remove('dark-mode')
    }
  }

  mediaQuery.addListener(handleThemeChange)
  handleThemeChange(mediaQuery)
</script>
</body>
</html>